/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import API.MenuBar;
import API.TabMenu;
import API.TextBox;
import API.TextType;
import API.Title;
import Core.MessageBuilder;
import Core.MessageType;
import Core.ReaderThread;
import Core.StringHelper;
import Yahoo.Group;
import Yahoo.Nick;
import Yahoo.StatusType;
import Yahoo.TextDisplay;
import Yahoo.TreeYahoo;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author mrbao
 */
public class ChatForm extends Canvas implements Runnable {

    private TreeYahoo tree;
//    private static Title title;
    private static MenuBar menuBar;
    private static TabMenu tabMenu;
    private static int width;
    private static int height;
    private Vector groups = new Vector();
    private boolean isShowPopupMenu;
    public static Vector tabs = new Vector();
    public static Vector chats = new Vector();
    private Timer timer;
    private TimerTask timerTask;
    private static boolean isRepaint;

    public ChatForm() {
        this.setFullScreenMode(true);
        width = getWidth();
        height = getHeight();
        menuBar = new MenuBar("Menu", "Thoat", "", width, height);
        int y = 0;
        tabs.addElement("Yahoo");
        tabMenu = new TabMenu(tabs, width, 0, y);
        y += tabMenu.getHeight();
        tree = new TreeYahoo(groups, 0, y, width, height - menuBar.getHeight() - tabMenu.getHeight());
        new Thread(this).start();
        timer = new Timer();
        timerTask = new TimerTask() {

            public void run() {
                while (true) {
                    try {
                        Thread.sleep(100);
                        if (isRepaint) {
                            repaint();
                            isRepaint = false;
                        }
                    } catch (Exception e) {
                    }
                }
            }
        };
        timer.schedule(timerTask, 0);
    }

    public static void AddChat(String nick) {
        if (tabs.contains(nick)) {
            tabMenu.setSelectedTab(tabs.indexOf(nick));
            return;
//            for (int i = 0; i < tabs.size(); i++) {
//                if (tabs.elementAt(i).toString().equals(nick)) {
//                    tabMenu.setSelectedTab(i);
//                    return;
//                }
//            }
        }
        tabs.addElement(nick);
        tabMenu.SetTabs(tabs);
        tabMenu.Go(tabs.size() - 2);
        tabMenu.setSelectedTab(tabs.size() - 1);
        TextBox textBox = new TextBox("", TextType.ANY, 2, height - menuBar.getHeight() - 1, width - 4);
        textBox.setY(textBox.getY() - textBox.getHeight());
        TextDisplay textDisplay = new TextDisplay(0, tabMenu.getHeight(), width, height - menuBar.getHeight() - textBox.getHeight() - tabMenu.getHeight());
        chats.addElement(textDisplay);
        chats.addElement(textBox);
        isRepaint = true;
    }

    protected void paint(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, width, getHeight());
        if (!isShowPopupMenu) {
            if (tabMenu.getSelectedTab() == 0) {
                tabMenu.Paint(g);
                tree.Paint(g);
            } else {
                TextDisplay textDisplay = (TextDisplay) chats.elementAt((tabMenu.getSelectedTab() - 1) * 2);
                textDisplay.Paint(g);
                TextBox textBox = (TextBox) chats.elementAt((tabMenu.getSelectedTab() - 1) * 2 + 1);
                textBox.setIsSelect(true);
                textBox.Paint(g);
                tabMenu.Paint(g);
            }
            menuBar.Paint(g);
        }
    }

    protected void keyPressed(int keyCode) {
        int gameCode = getGameAction(keyCode);
        switch (gameCode) {
            case RIGHT:
                if (tabs.size() > 0) {
                    tabMenu.KeyPress(gameCode);
                }
                repaint();
                break;
            case LEFT:
                if (tabs.size() > 0) {
                    tabMenu.KeyPress(gameCode);
                }
                repaint();
                break;
            case DOWN:
            case UP:
                repaint();
                if (tabMenu.getSelectedTab() == 0) {
                    tree.KeyPress(keyCode, gameCode);
                } else {
                    TextDisplay textDisplay = (TextDisplay) chats.elementAt((tabMenu.getSelectedTab() - 1) * 2);
                    textDisplay.KeyPress(gameCode);
                }
                break;
            case FIRE:
                if (tabMenu.getSelectedTab() == 0) {
                    tree.KeyPress(keyCode, gameCode);
                } else {
                    TextDisplay textDisplay = (TextDisplay) chats.elementAt((tabMenu.getSelectedTab() - 1) * 2);
                    TextBox textBox = (TextBox) chats.elementAt((tabMenu.getSelectedTab() - 1) * 2 + 1);
                    ChatYahoo.slaveClient.SendTextMessage(tabs.elementAt(tabMenu.getSelectedTab()).toString(), textBox.getText());
                    textDisplay.AddMessage("-" + textBox.getText());
                    textBox.setText("");
                }
                repaint();
                break;
        }
        if (tabMenu.getSelectedTab() > 0) {
            TextBox textBox = (TextBox) chats.elementAt((tabMenu.getSelectedTab() - 1) * 2 + 1);
            textBox.KeyPress(keyCode, gameCode);
            repaint();
        }
    }

    public void run() {
        ReaderThread readerThread = ChatYahoo.slaveClient.GetReaderThread();
        while (true) {
            String message = readerThread.GetFirst();
            if (message != null) {
                ProcessMessage(message);
                repaint();
            }
        }
    }

    private void ProcessMessage(String message) {
        if (message.startsWith(MessageType.FriendList)) {
            ProcessFriendList(message);
        } else if (message.startsWith(MessageType.ListStatus)) {
            ProcessListStatus(message);
        } else if (message.startsWith(MessageType.FriendReplyMessage)) {
            ProcessFriendReplyMessage(message);
        }
    }

    private void ProcessFriendReplyMessage(String message) {
        message = message.substring(message.indexOf(MessageBuilder.ONE) + 1);
        String[] allStr = new String[2];
        allStr[0] = message.substring(0, message.indexOf(";"));
        allStr[1] = message.substring(message.indexOf(";") + 1);
        if (tabs.contains(allStr[0])) {
            for (int i = 1; i < tabs.size(); i++) {
                if (tabs.elementAt(i).toString().equals(allStr[0])) {
                    TextDisplay textDisplay = (TextDisplay) chats.elementAt((i - 1) * 2);
                    tabMenu.setSelectedTab(i);
                    textDisplay.AddMessage("+" + allStr[1]);
                }
            }
        } else {
            tabs.addElement(allStr[0]);
            tabMenu.SetTabs(tabs);
            TextBox textBox = new TextBox("", TextType.ANY, 2, height - menuBar.getHeight() - 1, width - 4);
            textBox.setY(textBox.getY() - textBox.getHeight());
            TextDisplay textDisplay = new TextDisplay(0, tabMenu.getHeight(), width, height - menuBar.getHeight() - textBox.getHeight() - tabMenu.getHeight());
            chats.addElement(textDisplay);
            chats.addElement(textBox);
            textDisplay.AddMessage("+" + allStr[1]);
        }
    }

    private void ProcessListStatus(String message) {
        message = message.substring(message.indexOf(MessageBuilder.ONE) + 1);
        String[] allStr = StringHelper.split(message, ';');
        for (int i = 0; i < groups.size(); i++) {
            Group g = (Group) groups.elementAt(i);
            for (int j = 0; j < g.getAllNicks().size(); j++) {
                Nick n = (Nick) g.getAllNicks().elementAt(j);
                if (n.nick.equals(allStr[0])) {
                    n.status = allStr[2];
                    n.statusType = StatusType.AVAILABLE;
                    tree.setGroups(groups);
                }
            }
        }
    }

    private void ProcessFriendList(String message) {
        message = message.substring(message.indexOf(MessageBuilder.ONE) + 1);
        String[] allStr = StringHelper.split(message, ';');
        Group g = new Group(allStr[0]);
        for (int i = 1; i < allStr.length; i++) {
            String string = allStr[i];
            Nick nick = new Nick(string);
            nick.statusType = StatusType.OFFLINE;
            g.AddNick(nick);
        }
        groups.addElement(g);
        tree.setGroups(groups);
    }
}
